﻿using System;
using VIT.Web.Controls;
using VIT.Provider;
using System.Data;
using System.Collections.Generic;
using VIT.Library.Web;

namespace VIT.Web.Admin
{
    public partial class FileEdit : VIT.Web.UI.VITComponent
    {
        public string genOption = string.Empty;
        Provider.File obj;

        protected void Page_Load(object sender, EventArgs e)
        {
            Language language = new Language();
            obj = new Provider.File();
            try
            {
                obj.Id = int.Parse(Request["sId"]);
            }
            catch
            {
                obj.Id = -1;
            }

            if (obj.Id < 0) ButtonInsert.Visible = true; else ButtonModify.Visible = true;

            if (!IsPostBack)
            {
                loadData();
                loadLanguages();
                loadParent();
            }              
        }

        private void loadData()
        {
            obj.LanguageId = Session["language"].ToString();
            obj.GetSingle();

            if (obj.Id >0)
            {
                txtTitle.Text = obj.Title;
                pnFile.InnerHtml = "File: " + obj.Name + " (" + obj.Size + " bytes)";
                txtDescription.Text = obj.Description;
                txtOrder.Text = obj.Orders.ToString();
                chkPublish.Checked = obj.Published;
            }
        }

        private void loadLanguages()
        {
            ddlLang.DataSource = new Language().getAll();
            ddlLang.DataTextField = "Name";
            ddlLang.DataValueField = "Id";
            ddlLang.DataBind();

            ddlLang.SelectedValue = Session["language"].ToString();
        }

        private void loadParent()
        {
            Provider.Folder f = new Provider.Folder();
            f.LanguageId = Session["language"].ToString();
            this.genOption = f.GetAll().GenTagOption("Id", "Name", "0", selected: obj.FolderId.ToString());
        }

        private void save()
        {
            // bat loi tren server
            var data = new Dictionary<dynamic, List<object>> {
                                                        {txtTitle, new List<object> {"required"}},
                                                        {txtOrder, new List<object> {"required"}}
                                                    };
            if (MainCore.checkData(data))
            {
                bool checkType = true;
                obj.FolderId = Convert.ToInt32(Request["ParentId"]);

                if (fupFile.HasFile)
                {
                    //if (MainCore.checkFile(fupFile.FileName, "isDocument"))
                    //{
                        obj.Name = String.Format("{0:yyMMddhhmmss}", DateTime.Now) + System.IO.Path.GetExtension(fupFile.FileName);
                        string pathFile = "/Uploads/Documents/" + obj.Name;
                        fupFile.SaveAs(URLBaseDir + pathFile);
                        obj.Size = fupFile.FileContent.Length;
                    //}
                    //else checkType = false;
                }
                if (checkType)
                {
                    obj.LanguageId = ddlLang.SelectedValue;
                    obj.Title = txtTitle.Text;
                    obj.Description = txtDescription.Text;
                    obj.Orders = int.Parse(txtOrder.Text);
                    obj.Published = chkPublish.Checked;

                    if (obj.Save() > 0) setTrueVisible(lblThanhCong);
                    else setTrueVisible(lblThatBai);
                }
                else setTrueVisible(lblHinhKoDung);
            }
            else setTrueVisible(lblThatBai);
        }

        private void setTrueVisible(System.Web.UI.HtmlControls.HtmlGenericControl control)
        {
            lblThanhCong.Visible = false;
            lblThatBai.Visible = false;
            lblHinhKoDung.Visible = false;

            control.Visible = true;
        }

        protected void Save_Click(object sender, EventArgs e)
        {
            save();
            RedirectComponent("Document", "FileList");
        }

        protected void Apply_Click(object sender, EventArgs e)
        {
            save();
            loadData();
            loadParent();
        }

        protected void Cancel_Click(object sender, EventArgs e)
        {
            RedirectComponent("Document", "FileList");
        }

        protected void ddlLang_SelectedIndexChanged(object sender, EventArgs e)
        {
            Session["language"] = ddlLang.SelectedValue;
            loadData();
            loadParent();
        }
    }
}
